<?php


namespace App\Http\Controllers\Admin\Channel;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Services\Tools\ExportService;
use App\Http\Controllers\BaseController;

class ChannelController extends BaseController
{
     /**
      * @api {post} /channel 添加渠道
      *
      * @apiGroup 渠道
      *
      * @apiParam {string} channel_name 渠道名称，必须
      * @apiParam {string} remark 备注
      * @apiParam {string} color 渠道颜色，多个用英文逗号隔开
      *
      * @apiSuccessExample 成功响应
      * {"code":200,"data":[]}
      */
    public function add(Request $request)
    {
        $param = $request->only('channel_name', 'remark', 'color');
        $v = app('validator')->make($param, [
            'channel_name' => 'required|string|min:2|max:16',
            'remark' => 'string|max:20',
            'color' => 'string|max:15',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_channel')->addOrEdit($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '添加失败');
    }

    /**
     * @api {put} /channel 编辑渠道
     *
     * @apiGroup 渠道
     *
     * @apiParam {int} id 渠道id，必须
     * @apiParam {string} channel_name 渠道名称，必须
     * @apiParam {string} remark 备注
     * @apiParam {string} color 渠道颜色，多个用英文逗号隔开
     *
     * @apiSuccessExample 成功响应
     * {"code":200,"data":[]}
     */
    public function edit(Request $request)
    {
        $param = $request->only('id', 'channel_name', 'remark', 'color');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'channel_name' => 'required|string|min:2|max:16',
            'remark' => 'string|max:20',
            'color' => 'string|max:15',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $res = app('repo_channel')->addOrEdit($param);
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '编辑失败');
    }

    /**
     * @api {delete} /channel 删除渠道
     *
     * @apiGroup 渠道
     *
     * @apiParam {int} id 渠道id，必须
     *
     * @apiSuccessExample 成功响应
     * {"code":200,"data":[]}
     */
    public function del(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $channel = app('model_channel')->select('id')->where('id', $param['id'])->first();
        if (empty($channel)) throw new BasicException(0, '参数错误，渠道不存在');

        $channel->is_del = 1;
        $res = $channel->save();
        if ($res) {
            return Response::sendData([]);
        }
        throw new BasicException(0, '删除失败');
    }

    /**
     * @api {get} /channel 渠道列表
     *
     * @apiGroup 渠道
     *
     * @apiParam {int} id 渠道id
     * @apiParam {string} channel_name 渠道名称
     * @apiParam {int} page 第几页
     * @apiParam {int} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "id": 1, 渠道id
    "name": "头条", 渠道名称
    "remark": "备注", 备注
    "color": "#000000", 颜色
    "created_at": "2021-08-26 17:39:05", 创建时间
    "register_user_count": "252656", 注册用户数量
    "activated_user_count": "252656", 激活用户数量
    "recharge_user_count": "2708", 充值成功用户数
    "recharge_total": "843224" 充值合计
    }
    ],
    "first_page_url": "http://local.channel/channel?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.channel/channel?page=1",
    "next_page_url": null,
    "path": "http://local.channel/channel",
    "per_page": 10,
    "prev_page_url": null,
    "to": 2,
    "total": 2
    }
    }
     */
    public function list(Request $request)
    {
        $param = $request->only('id', 'channel_name', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $list = app('repo_channel')->channelList($param);
        return Response::sendData($list);
    }

    /**
     * @api {get} /channel/export 渠道列表导出
     *
     * @apiGroup 渠道
     *
     * @apiParam {int} id 渠道id
     * @apiParam {string} channel_name 渠道名称
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "download_url": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/data_excel/渠道列表20210903102529.xlsx"
    }
    }
     */
    public function listExport(Request $request)
    {
        $param = $request->only('id', 'channel_name', 'page', 'page_size');

        $list = app('repo_channel')->channelList($param, 0);

        $headings = [
            'id' => '渠道ID',
            'name' => '渠道名称',
            'remark' => '备注',
            'color' => '渠道用户进房颜色',
            'created_at' => '创建时间',
            'register_user_count' => '注册用户数量',
            'activated_user_count' => '激活用户数量',
            'recharge_user_count' => '充值成功用户数',
            'recharge_total' => '充值金额',
        ];

        $filename = '/channel/data_excel/' . 'channel' . date('YmdHis') . '.xlsx';
        $res = app('excel')->store(new ExportService($list, $headings), $filename, 'oss', 'Xlsx');
        if (!$res) throw new BasicException(0, '导出失败');

        return Response::sendData([
            "download_url" => 'https://'.env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').$filename,
        ]);
    }

    /**
     * @api {get} /channel/user 渠道用户列表
     *
     * @apiGroup 渠道
     *
     * @apiParam {string} register_time_start 注册时间开始，秒级时间戳
     * @apiParam {string} register_time_end 注册时间结束，秒级时间戳
     * @apiParam {string} last_login_time_start 最后登录时间开始，秒级时间戳
     * @apiParam {string} last_login_time_end 最后登录时间结束，秒级时间戳
     * @apiParam {int} channel_id 渠道ID
     * @apiParam {string} channel_name 渠道名称
     * @apiParam {string} mobiles 用户手机号，多个用英文逗号连接
     * @apiParam {int} page 第几页，默认1
     * @apiParam {int} page_size 每页显示条数，默认10
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "current_page": 1,
    "data": [
    {
    "user_id": 1309932, 用户索引id
    "mobile": "13018728993", 手机号
    "register_time": "2021-08-13 13:22:13", 注册时间
    "last_login_time": "2021-08-26 18:22:22", 最后登录时间
    "channel_id": 3, 渠道ID
    "keep_duration": 1141209,
    "user_code": 9414676, 用户ID
    "nickname": "宰父以轩941467", 昵称
    "recharge_total": "", 充值金额
    "earnings_total": "", 收益
    "watch_live_duration": "3.1", 观看直播时长，单位：小时
    "channel_name": "公众号" 渠道名称
    }
    ],
    "first_page_url": "http://local.channel/channel/user?page=1",
    "from": 1,
    "last_page": 1,
    "last_page_url": "http://local.channel/channel/user?page=1",
    "next_page_url": null,
    "path": "http://local.channel/channel/user",
    "per_page": 10,
    "prev_page_url": null,
    "to": 1,
    "total": 1
    }
    }
     */
    public function userList(Request $request)
    {
        $param = $request->only('register_time_start', 'register_time_end', 'last_login_time_start', 'last_login_time_end', 'channel_id', 'channel_name', 'mobiles', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'register_time_start' => 'integer',
            'register_time_end' => 'integer',
            'last_login_time_start' => 'integer',
            'last_login_time_end' => 'integer',
            'channel_id' => 'integer|min:1',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:100',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $list = app('repo_channel')->channelUserList($param);

        return Response::sendData($list);
    }

    /**
     * @api {get} /channel/user/export 渠道用户列表导出
     *
     * @apiGroup 渠道
     *
     * @apiParam {string} register_time_start 注册时间开始，秒级时间戳
     * @apiParam {string} register_time_end 注册时间结束，秒级时间戳
     * @apiParam {string} last_login_time_start 最后登录时间开始，秒级时间戳
     * @apiParam {string} last_login_time_end 最后登录时间结束，秒级时间戳
     * @apiParam {int} channel_id 渠道ID
     * @apiParam {string} channel_name 渠道名称
     * @apiParam {string} mobiles 用户手机号，多个用英文逗号连接
     *
     * @apiSuccessExample 成功响应
     * {
    "code": 200,
    "data": {
    "download_url": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/data_excel/渠道用户列表20210903102529.xlsx"
    }
    }
     */
    public function userListExport(Request $request)
    {
        ini_set('memory_limit', '2048M');

        $param = $request->only('register_time_start', 'register_time_end', 'last_login_time_start', 'last_login_time_end', 'channel_id', 'channel_name', 'mobiles');
        $v = app('validator')->make($param, [
            'register_time_start' => 'integer',
            'register_time_end' => 'integer',
            'last_login_time_start' => 'integer',
            'last_login_time_end' => 'integer',
            'channel_id' => 'integer|min:1',
        ]);
        if ($v->fails()) throw new BasicException(0, $v->errors()->first());

        $list = app('repo_channel')->channelUserList($param, 0);

        $headings = [
            '手机号',
            '注册时间',
            '最后登录时间',
            '渠道ID',
            '渠道名称',
            '用户ID',
            '昵称',
            '充值金额',
            '收益',
            '观看直播时长',
        ];
        $filename = '/channel/data_excel/' . 'channel_user' . date('YmdHis') . '.xlsx';
        $res = app('excel')->store(new ExportService($list, $headings), $filename, 'oss', 'Xlsx');
        if (!$res) throw new BasicException(0, '导出失败');

        return Response::sendData([
            "download_url" => 'https://'.env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').$filename,
        ]);
    }
}